package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.core.constant.UserConstants;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysPostService;
import org.asiainfo.system.domain.vo.SysPostVo;
import org.asiainfo.system.domain.bo.SysPostBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 岗位信息表
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/post")
public class SysPostController extends BaseController {

    private final SysPostService sysPostService;

    /**
     * 分页查询
     *
     * @param sysPostBo 筛选条件
     * @param pageQuery 分页对象
     * @return 查询结果
     */
    @SaCheckPermission("system:post:list")
    @GetMapping("/list")
    public TableDataInfo<SysPostVo> list(SysPostBo sysPostBo, PageQuery pageQuery) {
        return sysPostService.queryPageList(sysPostBo, pageQuery);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param postId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:post:query")
    @GetMapping("/{postId}")
    public R<SysPostVo> getInfo(@NotNull(message = "岗位ID不能为空") @PathVariable("postId") Long postId) {
        return R.ok(sysPostService.queryById(postId));
    }

    /**
     * 新增数据
     *
     * @param sysPostBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:post:add")
    @Log(title = "岗位表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPostBo sysPostBo) {
        if (sysPostService.checkPostNameUnique(sysPostBo)) {
            return R.fail("新增岗位'" + sysPostBo.getPostName() + "'失败，岗位名称已存在");
        } else if (sysPostService.checkPostCodeUnique(sysPostBo)) {
            return R.fail("新增岗位'" + sysPostBo.getPostName() + "'失败，岗位编码已存在");
        }
        return toAjax(sysPostService.insertByBo(sysPostBo));
    }

    /**
     * 编辑数据
     *
     * @param sysPostBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:post:edit")
    @Log(title = "岗位表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPostBo sysPostBo) {
        if (sysPostService.checkPostNameUnique(sysPostBo)) {
            return R.fail("修改岗位'" + sysPostBo.getPostName() + "'失败，岗位名称已存在");
        } else if (sysPostService.checkPostCodeUnique(sysPostBo)) {
            return R.fail("修改岗位'" + sysPostBo.getPostName() + "'失败，岗位编码已存在");
        } else if (UserConstants.POST_DISABLE.equals(sysPostBo.getPostStatus())) {
            if (sysPostService.countUserPostById(sysPostBo.getPostId()) > 0) {
                return R.fail("该岗位下存在已分配用户，不能禁用!");
            }
        }
        return toAjax(sysPostService.updateByBo(sysPostBo));
    }

    /**
     * 删除数据
     *
     * @param postIds 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:post:remove")
    @Log(title = "岗位表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public R<Void> remove(@NotNull(message = "岗位ID不能为空") @PathVariable Long[] postIds) {
        return toAjax(sysPostService.deleteWithValidByIds(List.of(postIds)));
    }

    /**
     * 获取岗位选择框列表
     */
    @SaCheckPermission("system:post:list")
    @GetMapping("/optionSelect")
    public R<List<SysPostVo>> optionSelect() {
        return R.ok(sysPostService.selectPostOptionSelect());
    }
}

